<?php
include_once('../../php/postgre.php');

$act = '';
if(isset($_POST['act'])) $act = $_POST['act'];
if($act == 'save_program'){
	$idPro = $_POST['id_program'];
	$kodePro = $_POST['kodeProgram'];
	$namaPro = $_POST['namaProgram'];
	$idKla = $_POST['idKlasifikasi'];
	//echo $idKla;
	
	if(!is_numeric($idPro)){
		echo "Error invalid id.";
		exit;
	}
	
	if($idPro > 0){
		exec_query("Update mus_program set kode_program = '".$kodePro."', nama_program = '".$namaPro."', id_kla = '".$idKla."' WHERE id_program = ".$idPro."");
		echo 'success__';
		logs('Mengubah master Program','Kode program : '.$kodePro.'; Nama Program : '.$namaPro);
	}else{
		$maxid = get_maxid('id_program', 'mus_program');
		$kolom = explode('.', $kodePro);
		//$maxkolom = get_maxid('kolom', 'mus_program');
		exec_query("INSERT INTO mus_program(id_program, kode_program, nama_program, id_kla, kolom) VALUES (".$maxid.",'".$kodePro."','".$namaPro."',".$idKla.",".$kolom[count($kolom)-1].")");
		$stored = get_data('SELECT id_program FROM mus_program WHERE id_program = '.$maxid);
		if($stored['id_program'] == $maxid){
			echo 'success__';
			$idPro = $maxid;
			logs('Menambah master Program','Kode Program : '.$kodePro.'; Nama Program : '.$namaPro);
		}
	}
	echo $idPro.'__';
	view_program();
	
}else if($act == 'del_program'){
	$idPro = $_POST['idPro'];
	if(is_numeric($idPro)){
		$proo = get_data('select kode_program,nama_program from mus_program where id_program='.$idPro);
		exec_query("DELETE FROM mus_program WHERE id_program =".$idPro);
		echo 'success__';
		logs('Menghapus master Program','Kode Program : '.$proo['kode_program'].'; Nama Program : '.$proo['nama_program']);
	}else{
		echo 'Error';
	}
	
	echo $idPro.'__';
	view_program();
}else if($act=='edit_program'){
	$idPro = $_POST['idPro'];
	if(is_numeric($idPro)){
		$data = get_data("select id_program, id_kla, kode_program, nama_program from mus_program where id_program=".$idPro);
		$ress = implode($data,'__');
		print_r($ress);
	}else{
		echo $idPro;
		echo 'errori';
	}
}
function view_program(){
	$no = 1;
	$program = get_datas("SELECT p.id_program, p.kode_program, p.nama_program, k.nama_klasifikasi FROM mus_program p, mus_klasifikasi k WHERE p.id_kla = k.id_klasifikasi order by id_kla, kolom");
	foreach($program as $program){
	?>
	<tr>
		<td style="text-align:center;"><?php echo $no ?></td>
		<td><?php echo $program['nama_klasifikasi'] ?></td>
		<td><?php echo $program['kode_program'] ?></td>
		<td><?php echo $program['nama_program'] ?></td>
		<td><a href="#modalwin" data-toggle="modal" style="text-align:center;" class="btn btn-info btn-small bt-edit-program" name="<?php echo $program['id_program']; ?>"><i class="icon-edit icon-white"></i>&nbsp; Ubah</a>
		<a class="btn btn-danger btn-small bt-delete-program" name="<?php echo $program['id_program']; ?>"><i class="icon-trash icon-white"></i>&nbsp; Hapus</a></td>
	</tr>
	<?php
	$no++;
	}
}
?>